from First.DataAccess import utils
from First import views
import requests

def xinyonghushouciliucun(start_date,end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'retaineduser/a&flag=newfirst&gran=day'
        metrics = 'new_user_count,retained_ratio'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwxinyonghushouciliucun')
    if 'error_code' not in str1:
        dataqingxi = {}
        dataqingxi['riqi'] = str1['result']['items'][0]
        dataqingxi['xinyonghushu'] = str1['result']['items'][1]
        for x1 in range(len(dataqingxi['xinyonghushu'])):
            for y1 in range(len(dataqingxi['xinyonghushu'][x1])):
                if (dataqingxi['xinyonghushu'][x1][y1] == '--'):
                    dataqingxi['xinyonghushu'][x1][y1] = 0
        return dataqingxi

def xinyonghuzidingyiliucun(start_date,end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'retaineduser/a&flag=newcustom&gran=day'
        metrics = 'retained_user_count,new_user_count,retained_ratio'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwxinyonghuzidingyiliucun')
    if 'error_code' not in str1:
        dataqingxi = {}
        dataqingxi['riqi'] = str1['result']['items'][0]
        dataqingxi['liucunyonghushu'] = []
        dataqingxi['xinyonghushu'] = []
        dataqingxi['liucunlv'] = []
        for num in range(0, len(str1['result']['items'][1])):
            dataqingxi['liucunyonghushu'].append(
                0 if str1['result']['items'][1][num][0] == "--" else int(str1['result']['items'][1][num][0]))
            dataqingxi['xinyonghushu'].append(
                0 if str1['result']['items'][1][num][1] == "--" else int(str1['result']['items'][1][num][1]))
            dataqingxi['liucunlv'].append(
                0 if str1['result']['items'][1][num][2] == "--" else int(str1['result']['items'][1][num][2]))
        return dataqingxi


def huoyueyonghuliucun(start_date,end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'retaineduser/a&flag=active&gran=day'
        metrics = 'active_user_count,retained_user_count'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwhuoyueyonghuliucun')
    if 'error_code' not in str1:
        dataqingxi = {}
        dataqingxi['riqi'] = str1['result']['items'][0]
        dataqingxi['huoyueyonghushu'] = str1['result']['items'][1]
        for x1 in range(len(dataqingxi['huoyueyonghushu'])):
            for y1 in range(len(dataqingxi['huoyueyonghushu'][x1])):
                if (dataqingxi['huoyueyonghushu'][x1][y1] == ''):
                    dataqingxi['huoyueyonghushu'][x1][y1] = 0
        return dataqingxi


def request_data(url):
    req = requests.get(url, timeout=30)  # 请求连接
    req_jason = req.json()  # 获取数据
    return req_jason